package com.lagou.edu.controller;

import com.lagou.edu.service.LagouCodeService;
import com.lagou.edu.service.LagouEmailService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
@RequestMapping("/api/code")
public class LagouCodeController {

    @Autowired
    private LagouCodeService lagouCodeService;

//    @Autowired
//    private LagouEmailServiceFeignClient lagouEmailServiceFeignClient;

    @Reference(check = false,version = "2.7.3")
    private LagouEmailService lagouEmailService;

    @GetMapping("/create/{email}")
    public boolean create(@PathVariable("email") String email) {
        Random random = new Random();
        String result = "";
        for (int i = 0; i < 6; i++) {
            result += random.nextInt(10);
        }

        lagouCodeService.create(email, result);
        //2.发送到邮箱
        return lagouEmailService.sendEmail(email, result);
    }

    @GetMapping("/validate/{email}/{code}")
    public int validate(@PathVariable("email") String email, @PathVariable("code") String code) {
        return lagouCodeService.validate(email, code);
    }


}
